package com.collection;

import java.util.LinkedList;

/**
 * 自定义map的升级版
 * 1.提高查询的效率
 * 底层实现：数组+链表
 *
 * @author jiangbang
 * @date 2016/5/30
 */
public class MyMap002 {
    MyLinkedList[] arr = new MyLinkedList[990];  //map的底层结构就是：数组+链表
    int size;

    public void put(Object key, Object value){
        MyEntry e = new MyEntry(key,value);

        int a =key.hashCode()%990;  //简单的算法，可以优化
        if(arr[a] == null){
            MyLinkedList list = new MyLinkedList();
            list.add(e);
        }else{
            arr[a].add(e);
        }
    }

    public Object get(Object key){
        int a = key.hashCode()%990;
        if(arr[a] != null){
            MyLinkedList list = arr[a];
            for(int i=0;i<list.size();i++){
                MyEntry e = (MyEntry)list.get(i);
                if(e.key.equals(key)){
                    return e.value;
                }
            }
        }
        return null;
    }
}
